
package projetocompiladores;

import java.awt.geom.Point2D;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jhotdraw.draw.Drawing;
import org.jhotdraw.draw.EllipseFigure;
import org.jhotdraw.draw.Figure;
import org.jhotdraw.geom.Geom;

/**
 *
 * @author Anderson
 */
public class StatementFigureUtils {

    public static Figure createIfStatementFigure(String text, Point2D.Double position) {
        return new LabeledDiamondFigure(text, position);
    }

    public static Figure createNormalStatementFigure(String text, Point2D.Double position) {
        return new LabeledRectangleFigure(text, position);
    }

    public static void createJunctionFigure(Drawing screen,
                                            Figure[] statementsAbove, Figure statementBelow) {

        /* Calcula a melhor posicao para a juncao */
        double x = 0.0;
        for(Figure figure : statementsAbove) {
            x += figure.getBounds().getX();
        }
        x /= statementsAbove.length;

        double y = statementsAbove[0].getBounds().getMaxY();
        for(Figure figure : statementsAbove) {
            if(y < figure.getBounds().getMaxY()) {
                y = figure.getBounds().getMaxY();
            }
        }
        y = (y + statementBelow.getBounds().getMinY()) / 2;
        
        /* Cria a juncao na posicao calculada */
        EllipseFigure junction = new EllipseFigure(x, y, 20, 20);
        screen.add(junction);

        /* Faz a conexao da juncao com as figuras */
        connectFiguresToJunctionFigure(screen, junction, statementsAbove, statementBelow);
    }

    private static void connectFiguresToJunctionFigure(Drawing screen, Figure junction,
                                                       Figure[] statementsAbove, Figure statementBelow) {

        LabeledBezierConnectionFigure[] connectionsFromAbove =
                new LabeledBezierConnectionFigure[statementsAbove.length];

        LabeledBezierConnectionFigure connectionToBelow = new LabeledBezierConnectionFigure();

        /* Conecta as figuras de cima com a juncao */
        for(int index = 0; index < statementsAbove.length; index++) {
            connectionsFromAbove[index] = new LabeledBezierConnectionFigure();

            connectionsFromAbove[index].setStartConnectorPosition(
                    Geom.south(statementsAbove[index].getBounds()));

            connectionsFromAbove[index].setEndConnectorPosition(Geom.north(junction.getBounds()));

            try {
                connectionsFromAbove[index].draw();
            } catch (Exception ex) {
                Logger.getLogger(StatementFigureUtils.class.getName()).log(Level.SEVERE, null, ex);
            }

            screen.add(connectionsFromAbove[index]);
        }

        /* Conecta a juncao com a figura abaixo */
        connectionToBelow = new LabeledBezierConnectionFigure();

        connectionToBelow.setStartConnectorPosition(Geom.south(junction.getBounds()));
        connectionToBelow.setEndConnectorPosition(Geom.north(statementBelow.getBounds()));

        try {
            connectionToBelow.draw();
        } catch (Exception ex) {
            Logger.getLogger(StatementFigureUtils.class.getName()).log(Level.SEVERE, null, ex);
        }

        screen.add(connectionToBelow);
    }
    public static void connectFiguresToIfStatement(Drawing screen, Figure ifStatement,
                                                   Figure trueStatement, Figure falseStatement) {

        LabeledBezierConnectionFigure trueBezierConnection;
        LabeledBezierConnectionFigure falseBezierConnection;

        if(trueStatement != null) {
            trueBezierConnection = new LabeledBezierConnectionFigure();

            trueBezierConnection.setLabel("true");
            trueBezierConnection.setLabelPosition(LabeledBezierConnectionFigure.Position.ESQUERDA);

            trueBezierConnection.setStartConnectorPosition(Geom.west(ifStatement.getBounds()));

            double decisionCenterY = ifStatement.getBounds().getCenterY();
            double trueCenterX = trueStatement.getBounds().getCenterX();
            Point2D.Double middlePoint = new Point2D.Double(trueCenterX, decisionCenterY);
            trueBezierConnection.addNode(middlePoint);

            trueBezierConnection.setEndConnectorPosition(Geom.north(trueStatement.getBounds()));

            try {
                trueBezierConnection.draw();
            } catch (Exception ex) {
                Logger.getLogger(StatementFigureUtils.class.getName()).log(Level.SEVERE, null, ex);
            }

            screen.add(trueBezierConnection);
        }

        if(falseStatement != null) {
            falseBezierConnection = new LabeledBezierConnectionFigure();

            falseBezierConnection.setLabel("false");
            falseBezierConnection.setLabelPosition(LabeledBezierConnectionFigure.Position.DIREITA);

            falseBezierConnection.setStartConnectorPosition(Geom.east(ifStatement.getBounds()));

            double decisionCenterY = ifStatement.getBounds().getCenterY();
            double falseCenterX = falseStatement.getBounds().getCenterX();
            Point2D.Double middlePoint = new Point2D.Double(falseCenterX, decisionCenterY);
            falseBezierConnection.addNode(middlePoint);

            falseBezierConnection.setEndConnectorPosition(Geom.north(falseStatement.getBounds()));

            try {
                falseBezierConnection.draw();
            } catch (Exception ex) {
                Logger.getLogger(StatementFigureUtils.class.getName()).log(Level.SEVERE, null, ex);
            }

            screen.add(falseBezierConnection);
        }
    }
}
